﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Xuanren_FrontEnd.Model;
using static System.Collections.Specialized.BitVector32;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class NewsController : Controller
    {
        private XuanrenTherapyEntities db  =new XuanrenTherapyEntities();
        // GET: News
        public ActionResult Index()
        {
             
            var list = db.NewsType.ToList();//获取到新闻分类
            return View(list);
        }
        [HttpPost]
        public ActionResult Search(int page, int limit, string title, string typeId, string begin, string end)
        {
            

            var q = db.view_NewsAndNewsType.Where(x => true);
            //这里实现多条件查询
            if (!string.IsNullOrWhiteSpace(title))
            {
                q = q.Where(x => x.title.Contains(title));//根据标题模糊查询
            }
            if (!string.IsNullOrWhiteSpace(typeId))
            {
                var Id = Convert.ToInt32(typeId);
                q = q.Where(x => x.typeId == Id);//根据新闻类型查询
            }
            if (!string.IsNullOrWhiteSpace(begin))
            {
                var b = Convert.ToDateTime(begin);
                q = q.Where(x => x.create_time >= b);//开始时间
            }
            if (!string.IsNullOrWhiteSpace(end))
            {
                var e = Convert.ToDateTime(end);
                q = q.Where(x => x.create_time <= e);//结束时间
            }
            var count = q.Count();//得到总统计数
            //根据时间排序，并实现分页
            var list = q.OrderByDescending(x => x.create_time).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
            {
                id = x.nid,
                title = x.title,
                content = x.content,
                create_time = x.create_time.ToString(),
                isShowHome = x.isShowHome,
                create_user = x.create_user,
                typeId = x.TypeName,
                defaultImg = x.defaultImg,

            });
            return Json(new { code = 0, msg = "", count = count, data = list });
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
             
            var list = db.NewsType.ToList();//获取到新闻分类
            return View(list);
        }
        [HttpPost]
        public ActionResult Add(News news)
        {
           
            {
                var username = Session["username"].ToString();

                news.create_time = DateTime.Now;

                news.create_user = username;

                /* News n = new News();//创建一个新闻对象
                 n.title = news.title;//新闻标题
                 n.content = news.content;//新闻内容
                 n.create_time = news.create_time;//创建时间              
                 n.isShowHome = news.isShowHome;
                 n.typeId = news.typeId;//新闻分类
                 n.create_user = news.create_user;
                 n.defaultImg = news.defaultImg;*/

                db.News.Add(news);//添加新闻
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { suc = true, msg = "添加成功" });
                }
                else
                {
                    return Json(new { suc = false, msg = "添加失败" });

                }
            }
        }

        public ActionResult Updata(int id)
        {
            
            ViewBag.NewsType = db.NewsType.ToList();//类型传值，可以将值传入到页面当中
            var obj = db.News.SingleOrDefault(x => x.id == id);

            return View(obj);//强类型传值
        }
        [HttpPost]
        public ActionResult Update(News news)
        {
            var username= Session["username"].ToString();

             
            var obj = db.News.SingleOrDefault(x => x.id == news.id);//获取指定Id的新闻对象
            obj.title = news.title;

            obj.content = news.content;

            obj.create_time = DateTime.Now;

            obj.create_user = username;

            obj.isShowHome = news.isShowHome;
            
            obj.typeId = news.typeId;
            obj.defaultImg = news.defaultImg;

            int r = db.SaveChanges();
            if (r > 0)
            {
                return Json(new { suc = true, msg = "修改成功" });
            }
            else
            {
                return Json(new { suc = false, msg = "修改失败" });

            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delect(int id)
        {
             
            var obj = db.News.SingleOrDefault(x => x.id == id);
            if (obj != null)
            {
                db.News.Remove(obj);//删除该新闻
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败" });
                }
            }
            else
            {
                return Json(new { code = -1, msg = "出现未知异常" });
            }
        }
    }
}
